package com.lzxzjy.web.servlet;

import com.alibaba.fastjson.JSON;
import com.lzxzjy.pojo.Announcement;
import com.lzxzjy.pojo.PageBean;
import com.lzxzjy.serivce.AnnouncementService;
import com.lzxzjy.serivce.impl.AnnouncementServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/announcement/*")
public class AnnouncementServlet extends BaseServlet {
    private final AnnouncementService announcementService = new AnnouncementServiceImpl();

    public void insertAnnouncement(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader announcementReader = request.getReader();
        String announcementLine = announcementReader.readLine();
        Announcement announcement = JSON.parseObject(announcementLine, Announcement.class);
        boolean isSuccess = announcementService.insertAnnouncement(announcement);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    private int[] toIntArrayAnnouncementIds(String announcementLine) {
        Announcement[] announcements = JSON.parseObject(announcementLine, Announcement[].class);
        StringBuilder stringAnnouncementIds = new StringBuilder("[");
        for (Announcement announcement : announcements) {
            stringAnnouncementIds.append(announcement.getAnnouncementId());
            stringAnnouncementIds.append(",");
        }
        stringAnnouncementIds.replace(stringAnnouncementIds.length() - 1, stringAnnouncementIds.length(), "]");
        return JSON.parseObject(String.valueOf(stringAnnouncementIds), int[].class);
    }

    public void deleteAnnouncementByAnnouncementIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader announcementReader = request.getReader();
        String announcementLine = announcementReader.readLine();
        int[] announcementIds = toIntArrayAnnouncementIds(announcementLine);
        boolean isSuccess = announcementService.deleteAnnouncementByIds(announcementIds);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void deleteAnnouncementByAnnouncementId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int announcementId = Integer.parseInt(request.getParameter("announcementId"));
        boolean isSuccess = announcementService.deleteAnnouncementById(announcementId);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void updateAnnouncementByAnnouncementId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader announcementReader = request.getReader();
        String announcementLine = announcementReader.readLine();
        Announcement announcement = JSON.parseObject(announcementLine, Announcement.class);

        boolean isSuccess = announcementService.updateAnnouncementById(announcement);
        if (isSuccess) {
            response.getWriter().write("success");
        } else {
            response.getWriter().write("failure");
        }
    }

    public void selectAnnouncementByPageAndTerms(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader announcementReader = request.getReader();
        String announcementLine = announcementReader.readLine();
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        Announcement announcement = JSON.parseObject(announcementLine, Announcement.class);
        announcement.setAnnouncementTitle("%" + announcement.getAnnouncementTitle() + "%");
        announcement.setAnnouncementContent("%" + announcement.getAnnouncementContent() + "%");
        PageBean<Announcement> announcementPageBean = announcementService.selectAnnouncementByPageAndTerms(announcement, currentPage, pageSize);
        String announcementPageBeanLine = JSON.toJSONString(announcementPageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(announcementPageBeanLine);
    }

    public void selectAllAnnouncement(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Announcement> announcements = announcementService.selectAllAnnouncement();
        String announcementsLine = JSON.toJSONString(announcements);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(announcementsLine);
    }
}
